<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="description" content="我的账本主页">

    <link rel="stylesheet" href="../weui/lib/weui.min.css">
    <link rel="stylesheet" href="../weui/css/jquery-weui.css">
    <link rel="stylesheet" href="../css/Gstyle.css">

    <title>主页-我的账本</title>
</head>
<body ontouchstart>
<div class="weui-tab">

    <div class="weui-tab__bd">

        <h1 style="margin-left: 10px;">增加消费记录</h1>
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">时间</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" id="time" type="text" value="" readonly="readonly">
                </div>
            </div>

            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">金额</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" id="money" type="number" placeholder="消费/收入金额">
                </div>
            </div>

            <div class="weui-cell">
                <div class="weui-cell__hd">
                    <label for="" class="weui-label">类型</label>
                </div>
                <div class="weui-cell__bd">
                    <input id="typeselect" class="weui-input" type="text" value="" placeholder="消费/收入类型" readonly="readonly"
                           onclick="showPicker()">
                    <input id="costtypeid" style="display:none" class="weui-input" type="text"
                           value="444deb215cd94e599b65e87524784735">
                </div>
            </div>

            <div style="height: 1px;border-top: 1px solid #D9D9D9;color: #D9D9D9;-webkit-transform-origin: 0 0;transform-origin: 0 0;-webkit-transform: scaleY(0.5);transform: scaleY(0.5);left: 15px;"></div>

            <div class="weui-cells__title" style="color: #000000;font-size: 17px;">备注</div>
            <div class="weui-cells weui-cells_form">
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <textarea id="textarea" class="weui-textarea" placeholder="请输入文本" rows="3"></textarea>
                        <!--<div class="weui-textarea-counter"><span>0</span>/200</div>-->
                    </div>
                </div>
            </div>
        </div>

        <div class="weui-btn-area">
            <a class="weui-btn weui-btn_primary" href="javascript:" id="showTooltips" onclick="addConsume()">保存</a>
        </div>

    </div>

    <div class="weui-tabbar">
        <a  class="weui-tabbar__item weui-bar__item--on" href="./index.html">
            <div class="weui-tabbar__icon">
                <img src="../img/icon_nav_msg.png" alt="">
            </div>
            <p class="weui-tabbar__label">增加记录</p>
        </a>
       <!-- <a class="weui-tabbar__item" href="addtype.html">
            <div class="weui-tabbar__icon">
                <img src="../img/icon_nav_article.png" alt="">
            </div>
            <p class="weui-tabbar__label">增加类型</p>
        </a>-->
        <a href="./showitem.html" class="weui-tabbar__item" >
            <div class="weui-tabbar__icon">
                <img src="../img/icon_nav_search_bar.png" alt="">
            </div>
            <p class="weui-tabbar__label">查看消费</p>
        </a>
        <a href="./setting.html" class="weui-tabbar__item">
            <div class="weui-tabbar__icon">
                <img src="../img/icon_nav_button.png" alt="">
            </div>
            <p class="weui-tabbar__label">设置</p>
        </a>
    </div>
</div>


<script src="../weui/lib/jquery-2.1.4.js"></script>
<!--<script src="../weui/lib/fastclick.js"></script>-->
<script src="../weui/js/jquery-weui.js"></script>
<script type="text/javascript" src="../weui/lib/weui.min.js"></script>
<script>

    function getLocation() {
        var options = {
            enableHighAccuracy: true,
            maximumAge: 1000
        }
        if (navigator.geolocation) {
            //浏览器支持geolocation
            navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
        } else {
            //浏览器不支持geolocation
        }
        $.hideLoading();
    }
    //成功时
    function onSuccess(position) {
        //返回用户位置
        //经度
        var longitude = position.coords.longitude;
        //纬度
        var latitude = position.coords.latitude;

        latlon = position.coords.latitude + ',' + position.coords.longitude;

        //baidu
        var url = "http://api.map.baidu.com/geocoder/v2/?ak=yVHqjQWMKk0kP5xr0e0Mi1P3OB1qy9ed&callback=renderReverse&location=" + latlon + "&output=json&pois=0&coordtype=wgs84ll";
        $.ajax({
            type: "GET",
            dataType: "jsonp",
            url: url,
            beforeSend: function () {
                $("#textarea").html('正在定位...');
            },
            success: function (json) {
                if (json.status == 0) {
                    $("#textarea").html(json.result.formatted_address);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $("#textarea").html(latlon + "地址位置获取失败");
            }
        });
    }
    //失败时
    function onError(error) {
        switch (error.code) {
            case 1:
                console.log("位置服务被拒绝");
                break;
            case 2:
                console.log("暂时获取不到位置信息");
                break;
            case 3:
                console.log("获取信息超时");
                break;
            case 4:
                console.log("未知错误");
                break;
        }
    }

    var typepickinfo;
    $(function () {
        var d = new Date();
        var str = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes();
        $("#time").val(str);

        $("#time").datetimePicker({
            title: '消费时间',
            min: "1990-12-12",
            max: "2100-12-12 12:12",
            onChange: function (picker, values, displayValues) {
                console.log(values);
            }
        });

//        $.showLoading("正在加载");

        setTimeout(function () {
            getLocation()
        }, 500);
    });
    function showPicker() {
        weui.picker([
                {
                    label: "收入",
                    value: "0",
                    children: [
                        {
                            label: "工资",
                            value: "01"
                        },
                        {
                            label: "收款",
                            value: "01"
                        },
                    ]
                },
                {
                    label: "吃喝",
                    value: "1",
                    children: [
                        {
                            label: "早餐",
                            value: "11"
                        },
                        {
                            label: "午餐",
                            value: "12"
                        },
                        {
                            label: "晚餐",
                            value: "13"
                        },
                        {
                            label: "夜宵",
                            value: "14"
                        },
                        {
                            label: "零食",
                            value: "15"
                        }
                    ]
                },
                {
                    label: "服装",
                    value: "2",
                    children: [
                        {
                            label: "衣服",
                            value: "21"
                        },
                        {
                            label: "裤子",
                            value: "22"
                        },
                        {
                            label: "鞋子",
                            value: "23"
                        },
                        {
                            label: "棉衣",
                            value: "24"
                        }
                    ]
                },
                {
                    label: "住宿",
                    value: "3",
                    children: [
                        {
                            label: "酒店",
                            value: "31"
                        },
                        {
                            label: "宾馆",
                            value: "32"
                        },
                        {
                            label: "旅社",
                            value: "33"
                        },
                        {
                            label: "民宿",
                            value: "34"
                        },

                    ]
                },
                {
                    label: "交通",
                    value: "4",
                    children: [
                        {
                            label: "火车",
                            value: "41"
                        },
                        {
                            label: "飞机",
                            value: "42"
                        },
                        {
                            label: "大巴",
                            value: "43"
                        },
                        {
                            label: "公交",
                            value: "44"
                        },
                        {
                            label: "地铁",
                            value: "44"
                        }
                    ]
                },
                {
                    label: "其他",
                    value: "5",
                    children: [
                        {
                            label: "其他",
                            value: "51"
                        },

                    ]
                }],
            {
                defaultValue: ["1", "11"],
                onConfirm: function onConfirm(result) {
                    $("#typeselect").val(result[0].label+'-'+result[1].label);
                    $("#costtypeid").val(result[1].value);
                },
                id: 'typeselect'
            }
        );
    }

    function addConsume() {
        var addhistory=JSON.parse(window.localStorage.getItem('consume'))||[];
        var addHis=[],a={};
        var addTime=$('#time').val();
        var addType=$('#typeselect').val();
        var addMoney=$('#money').val();
        var addTxt=$('#textarea').val()
        a.time=addTime;
        a.addType=addType;
        a.addMoney=addMoney;
        a.addTxt=addTxt;
        if(addTime==''||addType==''||addMoney==''||addTxt==''){
            $.notification({
                title: "信息填写不完整",
                time:1000,
            });
            return
        }
        $.showLoading("正在加载");
        addhistory.push(a);
        window.localStorage.setItem('consume',JSON.stringify(addhistory))
        setTimeout(function () {
            $.hideLoading();
            window.location.reload()
        },1000)
    }
</script>



</body>
</html>
